package com.bhf.entities;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.ImageObserver;
import java.io.File;

import javax.swing.ImageIcon;

import com.bhf.entities.Plane.ImageObserverAdapter;
import com.bhf.frame.MainFrame;
import com.bhf.listener.EnemyPlaneListener;

public class EnemyPlane {
	// 飞机的宽度
	public static int PLANE_WIDTH = 15; // 55
	public static int PLANE_HEIGHT = 15;// 38
	// 敌人飞机的位置
	private int left;
	private int height;
	// 炮弹速度,默认移动1个像素
	private int speed=0;
	// 对于主窗口的引用
	private MainFrame frame;

	// 颜色
	private Color color;

	private Image imageIcon = null;

	// 监听器
	private EnemyPlaneListener listener;

	public EnemyPlane() {
	}

	public EnemyPlane(MainFrame frame, Color color, Image img, double half) {
		this.frame = frame;
		this.color = color;
		imageIcon =img; // 没加缓冲
		PLANE_WIDTH = (int)((imageIcon.getWidth(null))*half);
		PLANE_HEIGHT = (int)((imageIcon.getHeight(null))*half);
	}

	public EnemyPlane(MainFrame frame, Color color, String img, double half) {
		this.frame = frame;
		this.color = color;
		imageIcon = new ImageIcon(img).getImage(); // 没加缓冲
		PLANE_WIDTH = (int)((imageIcon.getWidth(null))*half);
		PLANE_HEIGHT = (int)((imageIcon.getHeight(null))*half);
//		PLANE_WIDTH = (int)((imageIcon.getWidth(null))*0.5);
//		PLANE_HEIGHT = (int)((imageIcon.getHeight(null))*0.5);
		// PLANE_WIDTH = 28;
		// PLANE_HEIGHT = 19;
	}

	/**
	 * 取得敌机的区域
	 * 
	 * @return
	 */
	public Rectangle getRectangle() {
		Rectangle rec = new Rectangle((int) (left + (PLANE_WIDTH * 0.2)), (int) (height + PLANE_HEIGHT * 0.1), (int) (PLANE_WIDTH * 0.8),
				(int) (PLANE_HEIGHT * 0.9));
		return rec;
	}

	/**
	 * 注册监听器
	 * 
	 * @param listener
	 */
	public void addEnemyPlaneListener(EnemyPlaneListener listener) {
		this.listener = listener;
	}

	/**
	 * 画出自己
	 * 
	 * @param g
	 */
	public void draw(Graphics g) {
		g.setColor(this.color);
		// g.fillRect(left, height, PLANE_WIDTH, PLANE_HEIGHT);
		g.drawImage(imageIcon, left, height, PLANE_WIDTH, PLANE_HEIGHT, new ImageObserverAdapter());

	}

	public int getLeft() {
		return left;
	}

	public void setLeft(int left) {
		this.left = left;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int getSpeed() {
		return speed;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}

	public MainFrame getFrame() {
		return frame;
	}

	class ImageObserverAdapter implements ImageObserver {
		@Override
		public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
			return false;
		}

	}
}
